/**
 * Created by mapbar_front on 2019/5/28.
 */
import React,{ Component } from 'react';
sayName();

function sayName(){

}
console.log(a);
var a = 3;

//函数表达式和其他表达式一样，在使用前必须先赋值。
//say();
//var say = function(){} //TypeError: say is not a function

var condition = true;
if(condition) {
    function sayHi(){
        console.log('hi');
    }
    sayHi();
}else {
    function sayHi(){
        console.log('hello');
    }
    sayHi();
}

var say;
if(condition){
    say = function say(){
        console.log('tianxia');
    }
    say();
}else {
    say = function say(){
        console.log('xiao');
    }
    say()
}

function sum(num1,num2){
    return num1 + num2;
}
console.log(sum(10,20));
var anotherSum = sum;
sum = null;
console.log(anotherSum(10,20));
console.log(sum);

//将函数名想象为指针，也有助于理解为什么ECMAScript没有重载的概念。
function addSomeNumber(num){
    return num + 100;
}
function addSomeNumber(num){
    return num + 200;
}
console.log(addSomeNumber(100)); //300

var addSomeNumber = function(num){
    return num + 200;
}
var addSomeNumber = function(num){
    return num + 400;
}
console.log(addSomeNumber(100)); //300

console.log(add(10,20));
function add(num1,num2){
    return num1 + num2;
}

//add1(1,2); //TypeError: add1 is not a function
var add1 = function(num1,num2){
    return num1 + num2;
}

/*
//var eval = 1;
//var arguments = 1;
let arr = [[1,2,3],[3,4,4,5,5],[6,7,8,9,[11,12,[12,13,[14]]]],10];
//数组换成你的数组
function mapArr(arr) {
    let newArr = [];
    for(var i=0;i<arr.length;i++) {
        if(Array.isArray(arr[i])) {
            let dealArr = this.mapArr(arr[i]);
            newArr = [...newArr,...dealArr];
        }else {
            newArr.push(arr[i]);
        }
    }

    return newArr;
}
console.log(mapArr(arr));

function removeRepetition(str) {
    if(str != ''){
        var result = str[0];
        for(var i = 1;i < str.length;i++){
            if(str[i] != str[i-1] ){
                result += str[i];
            }
        }
    }
    else result = '';

    return result;
}
console.log(removeRepetition("aaa"));

var z = 10;
function foo(){
    console.log(z);
}
(function(funArg){
    var z = 20;
    funArg();
})(foo)

var data = [];
for(var k=0;k<3;k++){
    data[k] = function(){
        console.log(k);
    }
}
data[0]();
data[1]();
data[2]();

var a = 100;
function testResult(){
    var b = 2*a;
    var a = 200;
    var c = a/2;
    console.log(b);
    console.log(c);
}
testResult();

function reverseString2(str){
    var newStr = "";
    for(var i=str.length-1; i>=0; i--){
        newStr += str[i];
    }
    return newStr;
}
console.log(reverseString2("helloworld"));

function reverseString(str){
    var array = str.split('');  //转换成字符串数组
    array = array.reverse();
    str = array.join('');
    return str;
}

function c(n){
    //判断数组里是否包含一个某一项值
    function contains(arr,item){
        var flag=false;
        for(var i=0;i<arr.length;i++){
            if(arr[i]==item){
                flag=true;
            }
        }
        return flag;
    }
    //创建随机数组
    function arrCreate(arr,n){
        while(arr.length<n){
            item=Math.floor(Math.random()*(32-2)+2)
            if(!contains(arr,item)){
                arr.push(item);
            }
        }
    }
    //判断参数类型
    function type(n){
        var class2type = {}
        var objs = "Boolean Number String Function Array Date RegExp Null Undefined".split(" ");
        for (var i = 0, l = objs.length; i < l; i++) {
            class2type[ "[object " + objs[i] + "]" ] = objs[i].toLowerCase();
        }
        return class2type[Object.prototype.toString.call(n)];
    }
    var arr=[];
    if(type(n)!='number'){
        throw("您传入的不是数字类型请传入数字类型的参数")
    }
    arrCreate(arr,n);
    return arr;
}
console.log(c(5));
*/

var cat = {
    eat: function(){
        console.log('猫咪喜欢吃鱼！');
    }
}
var dog = {
    eat: function(){
        console.log('狗喜欢吃肉');
    }
}

function hobby(animal) {
    if(animal.eat instanceof Function){
        animal.eat()
    }
}
hobby(cat);
hobby(dog);

var person1 = {
    toLocaleString: function(){
        return 'xiao';
    },
    toString: function(){
        return 'xiao';
    }
}

var person2 = {
    toLocaleString: function(){
        return 'tianxia';
    },
    toString: function(){
        return 'tianxia666';
    }
}
var people = [person1,person2];
console.log(people);
console.log(people.toString());
console.log(people.toLocaleString());

var arr = [1,10,3,5,6,12];
var res = arr.sort(compare);
//如果compare(value1,value2)<0 value1排在value2的前面
//如果compare(value1,value2)>0 value2排在value1前面
function compare(value1,value2) {
    if(value1 < value2){
        return 1;
    }else if(value1 > value2){
        return -1;
    }
    return 0;
}
//console.log(res);
//splice
//删除 原数组改变
//第一个参数是要删除的位置，第二个参数是要删除的长度
var colors = ['yellow','blue','green','purple'];
var colors1 = colors;
console.log('删除的项',colors.splice(1,2));
console.log('colors',colors);
console.log('colors1',colors1);

//插入 可以向指定位置插入任意项，改变原数组，有三个参数
//1：指定的位置
//2：0（要删除的项数）默认为0
//3：插入的项
var colors = ['yellow','blue','green','purple'];
console.log(colors.splice(1,0,'xiao')); //[]
console.log(colors);

//替换 可以向指定的位置插入任何项，同时删除任何项
var colors = ['yellow','blue','green','purple'];
colors.splice(1,2,'xiaoxiao','tianxia');
console.log(colors);

var numbers = [1,1,5,2,3,5,68,8,2,3];
console.log(numbers.indexOf(2)); //3
console.log(numbers.lastIndexOf(2)); //8

var arr = [2,4,6,8,10];
console.log(arr);
var res = arr.every((item,index)=>{
    return item%2==0;
})
console.log('every',res);

var some = arr.some((item,index)=>{
    return item > 2;
})
console.log('some',some);

var arr = [2,3,4,5,1,3,4,5];
var filter = arr.filter((item,index)=>{
    return item > 2;
})
console.log('filter',filter);

var map = arr.map((item,index)=>{
    return item * 2;
})
console.log('map',map);
var res=[];
var forEach = arr.forEach((item,index)=>{
    res.push(item);
})
console.log('res',res);

//reduce和reduceRight
var arr = [1,2,3,4,5];
var reduce = arr.reduce((prev,cur)=>{
    return prev + cur;
})
console.log('reduce',reduce);
var reduceRight = arr.reduce((prev,cur)=>{
    return prev + cur;
});
console.log('reduceRight',reduceRight);

function callSomeFunction(someFunction,someArgument){
    return someFunction(someArgument);
}
function add10(num){
    return num + 10;
}
var result1 = callSomeFunction(add10,10);
console.log('result1',result1); //20

function greeting(name){
    return 'Hello,'+ name;
}
var result2 = callSomeFunction(greeting,'tianxia');
console.log(result2);
var data = [
    {
        name: 'tianxia',
        age: 22,
    },{
        name: 'xiaoxiao',
        age: 18
    }
]

function compare(name){
    return function(object1,object2){
        console.log('object1',object1);
        var value1 = object1[name];
        var value2 = object2[name];

        if(value1<value2){
            return -1;
        }else if(value1 > value2){
            return 1;
        }
        return 0;
    }
}
data.sort(compare('age'));
console.log(data);

//5.5.4 函数内部属性
// function dealArr(){
//     console.log(arguments.callee);
// }
// var arr1 = ['yellow'];
// dealArr(arr1);

// function factorial(n){
//     if(n<1){
//         return 1;
//     }else {
//         return n*factorial(n-1);
//     }
// }
// var result = factorial(4);
// console.log(result);
// var trueFactorial = factorial;
//重写
// function factorial(n) {
//     if(n<1){
//         return 1;
//     }else {
//         return n * arguments.callee(n-1);
//     }
// }
//
// var trueFactorial = factorial;
// factorial = function(){
//     return 0;
// }

//console.log(trueFactorial(5));

// function outer(){
//     inner()
// }
// function inner(){
//    console.log(inner.caller);
// }
// outer();

// function outer(){
//     inner();
// }
// function inner(){
//     console.log(arguments.callee.caller);
// }
// outer();

// function f1(){
//     console.log(this);
// }
// f1();

// function factorial(num){
//     if(num<1){
//         return 1;
//     }else {
//         return num * factorial(num-1);
//     }
// }
// console.log(factorial(5));
// var trueFactorial = factorial;
// factorial = null;
// console.log(trueFactorial(5));

// function factorial(num){
//     if(num<1){
//         return 1;
//     }else {
//         return num * arguments.callee(num-1);
//     }
// }
// console.log(factorial(5));
// var trueFactorial = factorial;
// factorial = null;
// console.log(trueFactorial(5));

var factorial = (function f(n){
    if(n<1){
        return 1;
    }else {
        return n * f(n-1);
    }
})

console.log(factorial(5));
var trueF = factorial;
factorial = null;
console.log(trueF(5));

// function f1(){
//     var n = 999;
//     nAdd = function(){ n+=1 }
//     function f2(){
//         console.log(n);
//     }
//     return f2;
//
// }
// var result = f1();
// result();
// nAdd();
// result();
//
// function f1(){
//     var n = 999;
//     return function f2(){
//         console.log(n);
//     }
// }
// f1()();


// function foo(){
//     var num = 1;
//     function bar(){
//         console.log('num',num);
//     }
// }
// foo();

/*
function makeAdder(x){
    return function(y){
        return x + y;
    }
}
var add5 = makeAdder(5);
var add10 = makeAdder(10);

console.log(add5(2));
console.log(add10(2));

//定义一个函数，求和
function addSum(a){
    return function(b){
        return a + b;
    }
}
var result = addSum(2)(3);
console.log(result);

function sum(){
    var num = arguments[0];
    if(arguments.length === 1){
        return function (sec){
            return num + sec;
        }
    }else {
        var num = 0;
        for(var i=0;i<arguments.length;i++){
            num = num + arguments[i];
        }
        return num;
    }
}

sum(2,3);
sum(2)(3);

function sum1(x,y){
    var n = x;
    if(y){
        return x + y;
    }else {
        return function(y){
            return n + y;
        }
    }
}
sum1(2,3);
sum1(2)(3);

*/


class FunExpress extends Component{
    render(){
        return(
            <div>FunExpress123</div>
        )
    }
}

export default FunExpress;
